/*
 * @Author: liuzibin 
 * @Date: 2023-07-24 00:34:51 
 * @Descroption: 创建商品
 */
const axios = require("axios");

async function createStore(params, token) {
  const regex = /<strong[^>]*>(.*?)<\/strong>/g;
  const matches = params.descriptHtml.match(regex);
  let storeInfo = "-";

  if (matches) {
    storeInfo = matches.map((match) => match.replace(/<[^>]+>/g, "").trim());
  }

  const response = await axios.post(
    "https://borderclear.cn/mer/store/product/create",
    {
      //首图
      image: params.imageList[0],
      // 轮播图
      slider_image: params.imageList,
      // 商品名称
      store_name: params.title,
      // 商品简介
      store_info: storeInfo,
      // 商品详情
      content: params.descriptHtml,
      // 商品分类ID
      cate_id: 341,
      // 商品单位
      unit_name: params.unitName,
      // 1.仅到店自提 2快递计价配送 3全国包邮
      delivery_way: ["2"],
      // 商品规格
      attrValue: [
        {
          // 规格图片
          image: params.imageList[0],
          // 售价
          price: params.price,
          // 成本价
          cost: 0,
          // 市场价
          ot_price: 0,
          // 会员价 固定为null
          svip_price: null,
          // 后端代码未用到
          select: false,
          // 库存
          stock: params.stock,
          // 卡密 固定为{}
          cdkey: {},
          // 编号
          bar_code: "",
          // 重量
          weight: 0,
          // 体积
          volume: 0,
        },
      ],
      // 是否支持退款
      refund_switch: 0,
    },
    {
      headers: {
        Accept: "application/json, text/plain, */*",
        Referer: "http://borderclear.cn/admin/merchant/list",
        "X-Token": token,
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "Content-Type": "application/json;charset=UTF-8",
      },
    }
  );
  
  console.log(response.data);

  return response.data.data.id;
}

module.exports = createStore;
